package com.joysuch.wwyt.core.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.core.bean.AuthDepartAndUrlBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.*;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.core.service.BaseRoleDataAuthService;
import com.joysuch.wwyt.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author : pp
 * @date : Created in 2021/4/13 11:16
 */
@RestController
@RequestMapping("/base/role/data/auth")
@Api(tags = "系统管理-角色数据权限")
public class BaseRoleDataAuthController {
    @Autowired
    private BaseRoleDataAuthService dataAuthService;
    @Autowired
    private BaseConfigService baseConfigService;
    @Autowired
    private BpDepartDao bpDepartDao;
    
    @RequiresPermissions("base:role:data:auth:depart:add")
    @PostMapping("depart/add")
    @ApiOperation("新增组织权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", paramType = "form", required = true),
            @ApiImplicitParam(name = "containChild", value = "是否包含子部门", paramType = "form", required = true),
            @ApiImplicitParam(name = "departId", value = "部门id", paramType = "form", required = true)
    })
    public ResultBean addDepartAuth(@Valid BaseRoleDataDepartAuth entity) {
        dataAuthService.saveDepartAuth(entity);
        return ResultBean.success("组织权限保存成功");
    }

    @RequiresPermissions("base:role:data:auth:depart:delete")
    @PostMapping("depart/delete")
    @ApiOperation("组织权限批量删除")
    public ResultBean deleteDepartAuth(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new IllegalArgumentException("请勾选删除项");
        }
        dataAuthService.deleteDepartAuth(ids);
        return ResultBean.success("组织权限删除成功");
    }

    @RequiresPermissions("base:role:data:auth:depart:findAll")
    @PostMapping("depart/findAll")
    @ApiOperation("组织权限查询")
    public ResultBean findAllDepartAuth(@Valid @RequestBody BaseDataAuthParam param) {
        PageInfo<BaseRoleDataDepartAuthVo> list = dataAuthService.findAllDepartAuth(param.getRoleId(), param.getPageNum()+1, param.getPageSize());
        return ResultBean.pageData(list.getLists(), list.getTotalCount());
    }

    @RequiresPermissions("base:role:data:auth:depart:emp:add")
    @PostMapping("emp/add")
    @ApiOperation("新增人员权限")
    public ResultBean saveEmpAuth(@Valid @RequestBody List<BaseRoleDataEmpAuth> entitys) {
        if (CollectionUtils.isEmpty(entitys)) {
            throw new IllegalArgumentException("至少选择一个人员");
        }
        dataAuthService.saveEmpAuth(entitys);
        return ResultBean.success("人员权限添加成功");
    }

    @RequiresPermissions("base:role:data:auth:depart:emp:delete")
    @PostMapping("emp/delete")
    @ApiOperation("人员权限批量删除")
    public ResultBean deleteEmpAuth(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            throw new IllegalArgumentException("请勾选删除项");
        }
        dataAuthService.deleteEmpAuth(ids);
        return ResultBean.success("人员权限删除成功");
    }

    @RequiresPermissions("base:role:data:auth:depart:emp:findAll")
    @PostMapping("emp/findAll")
    @ApiOperation("人员权限查询")
    public ResultBean findAllEmpAuth(@Valid @RequestBody BaseDataAuthParam param) {
        return dataAuthService.findAllEmpAuth(param.getRoleId(), param.getPageNum()+1, param.getPageSize());
    }

    @RequiresPermissions("base:role:data:auth:depart:emp:getUserIds")
    @GetMapping("emp/getUserIds")
    @ApiOperation("数据权限人员id查询")
    public ResultBean getUserIds(@NotNull(message = "角色id不能为空") Long roleId){
        Long[] ids = dataAuthService.getUserIds(roleId);
        return ResultBean.success(ids);
    }
    
    @GetMapping("depart/findAuthDepartIds")
    @ApiOperation("获取当前用户角色拥有的部门权限以及报表的url")
    public ResultBean findAuthDepartIdsByCurUser(){
        AuthDepartAndUrlBean result = new AuthDepartAndUrlBean();
        Set<Long> authDepartIds = dataAuthService.findAuthDepartIdsByCurUser();
        result.setAuthDepartIds(authDepartIds);
        BaseConfig firstConfig = baseConfigService.findFirstConfig(BaseConfigCodes.CENTER_REPORT_URL);
        String reportUrl = firstConfig != null ? firstConfig.getValue() : null;
        result.setCenterReportUrl(reportUrl);
        return ResultBean.success(result);
    }
    
    @GetMapping("depart/findAuthDepartSimpleSelect")
    @ApiOperation("获取当前用户角色拥有的部门权限id和name")
    public ResultBean findAuthDepartSimpleSelect(){
        Set<Long> authDepartIds = dataAuthService.findAuthDepartIdsByCurUser();
        TreeSet<Long> authDepartIdsTree = new TreeSet<>(authDepartIds);
        ArrayList<SimpleSelectListBean> select = new ArrayList<>();
        authDepartIdsTree.forEach(each->{
            select.add(new SimpleSelectListBean(each,bpDepartDao.getNameById(each)));
        });
        return ResultBean.success(select);
    }

    @GetMapping("depart/selectJobForReport")
    @ApiOperation("获取风险点的岗位下拉列表")
    public ResultBean selectJobForReport(){
        return dataAuthService.findJobForReport();
    }
}
